﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DbHelp;
using DevExpress.XtraTreeList.Nodes;

namespace IotherRis
{
    public partial class Uc_ReportTemplate : UserControl
    {
        public Uc_ReportTemplate()
        {
            InitializeComponent();
        }
        uDataSetC mUds = null;
        string mState = "";
        Fr_main_other mMain_other;

        public Fr_main_other MMain_other
        {
            get { return mMain_other; }
            set { mMain_other = value; }
        }


        TreeListNode mDeletNode = null;
        private void btn_query_Click(object sender, EventArgs e)
        {
            try
            {
                string mSql = @"select   IID,
                                       IPID,
                                       ISOURDID ,
                                       TEMPLATE_NAME ,
                                       TEMPLATE_DESC ,
                                       TEMPLATE_DIAG ,
                                       BODYPART,
                                       MODALITY
                                      
                                  from PI_REPORTTEMPLATE WHERE  1=1   ";
                // order by ISOURDID asc

                string mCondion = "";
                if (!string.IsNullOrEmpty(txt_modality.Text))
                {
                    mCondion = " and  MODALITY LIKE '%" + txt_modality.Text + "%' ";
                }

                if (!string.IsNullOrEmpty(txt_templatename.Text))
                {
                    mCondion = " and  TEMPLATE_NAME LIKE '%" + txt_templatename.Text + "%' ";
                }

                if (!string.IsNullOrEmpty(txt_bodypart.Text))
                {
                    mCondion = " and  bodypart LIKE '%" + txt_bodypart.Text + "%' ";
                }

                mSql = mSql + mCondion + " ";

                string mErr = "";
                uDataSetC mUds_load = new uDataSetC(mSql, AccessHelper.mReportDb);
                tree_template.KeyFieldName = "IID";
                tree_template.ParentFieldName = "IPID";
                tree_template.DataSource = mUds_load.Tables[0];
                if (!string.IsNullOrEmpty(mErr))
                {
                    MessageBox.Show("操作出现错误，查询中断！\r\n原因：\r\n" + mErr, "提示");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("操作出现错误，错误原因：\r\n" + ex.Message, "提示");
            }
        }

        private void uc_TreeListModify_DoubleClick(object sender, EventArgs e)
        {




            //Hashtable mHs = new Hashtable();
            //string mErr = "";
            //mHs.Add("模版名称", "CMBMC");
            //mHs.Add("部位名称", "CBWMC");
            //mHs.Add("部位编码", "CBWBM");
            //mHs.Add("结果归类", "CJGGL");
            //mHs.Add("报告描述", "CGCMS");
            //mHs.Add("报告诊断", "CZDJL");
            //mHs.Add("修改时间", "CGXSJ");
            //mHs.Add("备注", "CBZ");
            //mHs.Add("活动状态", "CFLAG");
            //mHs.Add("检查方法", "CJCFF");
            //mHs.Add("设备类型", "CSBLXBM");
            //mHs.Add("报告类型", "CBGLX");
            //mHs.Add("阳性率", "CYX");
            //if (!uc_TreeListModify.f_SetNode(mxg.mListDs, mHs, ref mErr))
            //{
            //    if (!string.IsNullOrEmpty(mErr))
            //    {
            //        XtraMessageBox.Show("当前操作异常！原因：\r\n" + mErr, "提示");
            //    }
            //}
        }

        private void btn_r_add_Click(object sender, EventArgs e)
        {


            if (mState == "修改中")
            {
                MessageBox.Show("请先保存当前编辑模版内容后在新增报告");
                return;
            }

            mUds = null;

            string msql = @"select * from pi_reporttemplate t where iid=-1";
            mUds = new uDataSetC(msql, AccessHelper.mReportDb);
            mUds.InsertRow();



            txt_r_bodypart.Text = "";
            txt_r_modality.Text = "";
            txt_r_template_desc.Text = "";
            txt_r_template_diag.Text = "";
            txt_r_template_name.Text = "";
            lab_ipid.Text = "";
            lab_iid.Text = "";
            mState = "修改中";


        }

        private void btn_r_save_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(mState))
            {
                MessageBox.Show("请先新增报告或编辑报告");
                return;
            }

            if (mUds == null)
            {
                MessageBox.Show("没有需要保存或新增的数据,请先新增或选择需要维护的报告模版");
                return;
            }

            try
            {
                mUds.SetItem(1, "BODYPART", txt_r_bodypart.Text);
                mUds.SetItem(1, "MODALITY", txt_r_modality.Text);
                mUds.SetItem(1, "TEMPLATE_DESC", txt_r_template_desc.Text);
                mUds.SetItem(1, "TEMPLATE_DIAG", txt_r_template_diag.Text);
                mUds.SetItem(1, "TEMPLATE_NAME", txt_r_template_name.Text);
                //mUds.SetItem(1, "IPID", lab_ipid.Text);
                // if (lab_iid.Text != "-1")
                // {
                //   mUds.SetItem(1, "IID", lab_iid.Text);
                // }


                mUds.Update(AccessHelper.mReportDb);
                mState = "已保存";
                MessageBox.Show("保存成功");

                MMain_other.f_loadAllTemplate();
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存失败" + ex.Message);
            }
        }

        private void btn_r_delete_Click(object sender, EventArgs e)
        {
            try
            {

                if (mUds == null)
                {
                    MessageBox.Show("请先选择需要删除的报告模版");
                    return;
                }
                else
                {
                    mUds.DeleteRow(1);
                    txt_r_bodypart.Text = "";
                    txt_r_modality.Text = "";
                    txt_r_template_desc.Text = "";
                    txt_r_template_diag.Text = "";
                    txt_r_template_name.Text = "";
                    lab_ipid.Text = "";
                    lab_iid.Text = "";
                    mState = "";
                    tree_template.DeleteNode(mDeletNode);
                    mUds.Update(AccessHelper.mReportDb);
                    MessageBox.Show("删除成功");
                    MMain_other.f_loadAllTemplate();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存失败");
            }
        }

        private void uc_TreeListModify_Click(object sender, EventArgs e)
        {


        }

        private void Fr_ReportTemplate_Load(object sender, EventArgs e)
        {

        }



        private void uc_TreeListModify_Load(object sender, EventArgs e)
        {

        }

        private void item_ipid0_Click(object sender, EventArgs e)
        {
            Fr_add_pid mPid = new Fr_add_pid();
            mPid.ShowDialog();
        }

        private void item_ipid_Click(object sender, EventArgs e)
        {


            string mIid = tree_template.FocusedNode["IID"].ToString();
            string mPid = tree_template.FocusedNode["IPID"].ToString();

            if (string.IsNullOrEmpty(mPid))
            {
                MessageBox.Show("新增节点请先选中父节点");
                return;
            }
            Fr_add_Node mNode = new Fr_add_Node(mIid, mPid);
            mNode.ShowDialog();
        }

        private void item_detail_Click(object sender, EventArgs e)
        {

            TreeListNode mFocused = tree_template.FocusedNode;
            if (mFocused != null)
            {
                string mIID = tree_template.FocusedNode["IID"].ToString();
                mDeletNode = tree_template.FocusedNode;
                string mXX = tree_template.FocusedNode["TEMPLATE_NAEM"].ToString();
                txt_template_pidname.Text = mXX;
                lab_ipid.Text = tree_template.FocusedNode["IID"].ToString();
                //加载模版信息
                string msql = @"select * from pi_reporttemplate t where iid=" + mIID + "";
                mUds = new uDataSetC(msql, AccessHelper.mReportDb);
                if (mUds.RowCount() >= 1)
                {
                    txt_r_bodypart.Text = mUds.GetItemString(1, "BODYPART");
                    txt_r_modality.Text = mUds.GetItemString(1, "MODALITY");
                    txt_r_template_desc.Text = mUds.GetItemString(1, "TEMPLATE_DESC");
                    txt_r_template_diag.Text = mUds.GetItemString(1, "TEMPLATE_DIAG");
                    txt_r_template_name.Text = mUds.GetItemString(1, "TEMPLATE_NAME");

                    lab_iid.Text = mUds.GetItemString(1, "IID");
                    mState = "修改中";

                }
                else
                {
                    MessageBox.Show("请先选择报告模版");
                }
            }
            else
            {
                MessageBox.Show("请先选择报告模版");
            }
        }

        private void item_update_Click(object sender, EventArgs e)
        {
            string mIID = tree_template.FocusedNode["IID"].ToString();


            //加载模版信息
            string msql = @"select * from pi_reporttemplate t where iid=" + mIID + "";
            mUds = new uDataSetC(msql, AccessHelper.mReportDb);
            if (mUds.RowCount() >= 1)
            {
                txt_r_bodypart.Text = mUds.GetItemString(1, "BODYPART");
                txt_r_modality.Text = mUds.GetItemString(1, "MODALITY");
                txt_r_template_desc.Text = mUds.GetItemString(1, "TEMPLATE_DESC");
                txt_r_template_diag.Text = mUds.GetItemString(1, "TEMPLATE_DIAG");
                txt_r_template_name.Text = mUds.GetItemString(1, "TEMPLATE_NAME");
                lab_ipid.Text = mUds.GetItemString(1, "IPID");
                lab_iid.Text = mUds.GetItemString(1, "IID");

            }
            else
            {
                MessageBox.Show("请先选择报告模版");
            }
        }

        private void tree_template_DoubleClick(object sender, EventArgs e)
        {
            string mIID = tree_template.FocusedNode["IID"].ToString();
            mDeletNode = tree_template.FocusedNode;
            //string mXX = tree_template.FocusedNode["TEMPLATE_NAEM"].ToString();
            //txt_template_pidname.Text = mXX;
            //lab_ipid.Text = tree_template.FocusedNode["IID"].ToString();
            //加载模版信息
            string msql = @"select * from pi_reporttemplate t where iid=" + mIID + "";
            mUds = new uDataSetC(msql, AccessHelper.mReportDb);
            if (mUds.RowCount() >= 1)
            {
                txt_r_bodypart.Text = mUds.GetItemString(1, "BODYPART");
                txt_r_modality.Text = mUds.GetItemString(1, "MODALITY");
                txt_r_template_desc.Text = mUds.GetItemString(1, "TEMPLATE_DESC");
                txt_r_template_diag.Text = mUds.GetItemString(1, "TEMPLATE_DIAG");
                txt_r_template_name.Text = mUds.GetItemString(1, "TEMPLATE_NAME");

                lab_iid.Text = mUds.GetItemString(1, "IID");
                mState = "修改中";
            }
            else
            {
                MessageBox.Show("请先选择报告模版");
            }
        }

        private void item_delete_Click(object sender, EventArgs e)
        {
            try
            {

                string mIid = tree_template.FocusedNode["IID"].ToString();
                TreeListNode mFource = tree_template.FocusedNode;
                if (mFource.HasChildren)
                {
                    MessageBox.Show("该节点下还有子节点,不能直接删除");
                    return;

                }

                string msql = @"select * from pi_reporttemplate t where iid=" + mIid + "";

                uDataSetC mUds_delte = new uDataSetC(msql, AccessHelper.mReportDb);
                if (mUds_delte.RowCount() >= 1)
                {
                    mUds_delte.DeleteRow(1);
                    mUds_delte.Update(AccessHelper.mReportDb);
                    //  tree_template.DeleteNode(mDeletNode);
                    mDeletNode = null;

                    tree_template.DeleteNode(mDeletNode);
                    MessageBox.Show("删除成功");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("删除失败");
            }
        }

        private void btn_add_Click(object sender, EventArgs e)
        {

            if (mState == "修改中")
            {
                MessageBox.Show("请先保存当前编辑的模版内容");
                return;
            }

            string msql = @"select * from pi_reporttemplate t where iid=-1";
            mUds = new uDataSetC(msql, AccessHelper.mReportDb);
            mUds.InsertRow();

            txt_r_bodypart.Text = "";
            txt_r_modality.Text = "";
            txt_r_template_desc.Text = "";
            txt_r_template_diag.Text = "";
            txt_r_template_name.Text = "";
            lab_iid.Text = "-1";
            mState = "修改中";
        }

        private void tree_template_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {


            //string mXX = tree_template.FocusedNode["TEMPLATE_NAME"].ToString();
            //txt_template_pidname.Text = mXX;
            //lab_ipid.Text = tree_template.FocusedNode["IID"].ToString();

        }

        private void item_template_Click(object sender, EventArgs e)
        {
            if (mState == "修改中")
            {
                MessageBox.Show("请先保存当前编辑模版内容后在新增报告");
                return;
            }

            mUds = null;

            string msql = @"select * from pi_reporttemplate t where iid=-1";
            mUds = new uDataSetC(msql, AccessHelper.mReportDb);
            mUds.InsertRow();



            txt_r_bodypart.Text = "";
            txt_r_modality.Text = "";
            txt_r_template_desc.Text = "";
            txt_r_template_diag.Text = "";
            txt_r_template_name.Text = "";
            lab_ipid.Text = "";
            lab_iid.Text = "";
            mState = "修改中";
        }


    }
}
